<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: Origami::Filter::LZW</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">Origami::Filter::LZW</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../files/origami/filters_rb.html">
                origami/filters.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                Object
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Class representing a filter used to <a href="LZW.html#M000316">encode</a>
and <a href="LZW.html#M000317">decode</a> data with <a
href="LZW.html">LZW</a> compression algorithm.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000317">decode</a>&nbsp;&nbsp;
      <a href="#M000316">encode</a>&nbsp;&nbsp;
      <a href="#M000315">new</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->
    <div id="includes">
      <h3 class="section-bar">Included Modules</h3>

      <div id="includes-list">
        <span class="include-name"><a href="../Filter.html">Filter</a></span>
      </div>
    </div>

    <div id="section">

    <div id="class-list">
      <h3 class="section-bar">Classes and Modules</h3>

      Class <a href="LZW/DecodeParms.html" class="link">Origami::Filter::LZW::DecodeParms</a><br />

    </div>




      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000315" class="method-detail">
        <a name="M000315"></a>

        <div class="method-heading">
          <a href="#M000315" class="method-signature">
          <span class="method-name">new</span><span class="method-args">(parameters = DecodeParms.new)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Creates a <a href="LZW.html#M000315">new</a> <a href="LZW.html">LZW</a> <a
href="../Filter.html">Filter</a>.
</p>
<table>
<tr><td valign="top"><em>parameters</em>:</td><td>A hash of filter options (ignored).

</td></tr>
</table>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000315-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000315-source">
<pre>
     <span class="ruby-comment cmt"># File origami/filters.rb, line 451</span>
451:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">parameters</span> = <span class="ruby-constant">DecodeParms</span>.<span class="ruby-identifier">new</span>)
452:         <span class="ruby-keyword kw">super</span>(<span class="ruby-identifier">parameters</span>)
453:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000317" class="method-detail">
        <a name="M000317"></a>

        <div class="method-heading">
          <a href="#M000317" class="method-signature">
          <span class="method-name">decode</span><span class="method-args">(string)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Decodes given data using <a href="LZW.html">LZW</a> compression method.
</p>
<table>
<tr><td valign="top"><em>stream</em>:</td><td>The data to <a href="LZW.html#M000317">decode</a>.

</td></tr>
</table>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000317-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000317-source">
<pre>
     <span class="ruby-comment cmt"># File origami/filters.rb, line 508</span>
508:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">decode</span>(<span class="ruby-identifier">string</span>)
509:        
510:         <span class="ruby-identifier">result</span> = <span class="ruby-value str">&quot;&quot;</span>
511:         <span class="ruby-identifier">bstring</span> = <span class="ruby-identifier">string2binary</span>(<span class="ruby-identifier">string</span>)
512:         <span class="ruby-identifier">codesize</span> = <span class="ruby-value">9</span>
513:         <span class="ruby-identifier">table</span> = <span class="ruby-identifier">clear</span>({})
514:         <span class="ruby-identifier">prevbyte</span> = <span class="ruby-keyword kw">nil</span>
515: 
516:         <span class="ruby-keyword kw">until</span> <span class="ruby-identifier">bstring</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-keyword kw">do</span>
517:           
518:           <span class="ruby-identifier">byte</span> = <span class="ruby-identifier">binary2byte</span>(<span class="ruby-identifier">bstring</span>, <span class="ruby-identifier">codesize</span>)
519: 
520:           <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">table</span>.<span class="ruby-identifier">size</span>
521:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">510</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">codesize</span> = <span class="ruby-value">10</span>
522:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">1022</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">codesize</span> = <span class="ruby-value">11</span>
523:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">2046</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">codesize</span> = <span class="ruby-value">12</span>
524:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">4094</span>
525:               <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">byte</span> <span class="ruby-operator">!=</span> <span class="ruby-constant">CLEARTABLE</span>
526:               <span class="ruby-keyword kw">then</span>
527:                 <span class="ruby-identifier">raise</span> <span class="ruby-constant">InvalidLZWDataError</span>, <span class="ruby-value str">&quot;LZW table is full and no clear flag was set&quot;</span>
528:               <span class="ruby-keyword kw">end</span>
529:           <span class="ruby-keyword kw">end</span>
530: 
531:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">byte</span> <span class="ruby-operator">==</span> <span class="ruby-constant">CLEARTABLE</span>
532:             <span class="ruby-identifier">codesize</span> = <span class="ruby-value">9</span>
533:             <span class="ruby-identifier">code</span> = <span class="ruby-constant">EOD</span>
534:             <span class="ruby-identifier">clear</span> <span class="ruby-identifier">table</span>
535:             <span class="ruby-identifier">prevbyte</span> = <span class="ruby-keyword kw">nil</span>
536:             <span class="ruby-keyword kw">redo</span>
537:           <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">byte</span> <span class="ruby-operator">==</span> <span class="ruby-constant">EOD</span>
538:             <span class="ruby-keyword kw">break</span>
539:           <span class="ruby-keyword kw">else</span>
540:             <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">prevbyte</span>.<span class="ruby-identifier">nil?</span>
541:               <span class="ruby-identifier">prevbyte</span> = <span class="ruby-identifier">byte</span>
542:               <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">table</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">byte</span>)
543:               <span class="ruby-keyword kw">redo</span>
544:             <span class="ruby-keyword kw">else</span>
545:               <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">table</span>.<span class="ruby-identifier">has_value?</span>(<span class="ruby-identifier">byte</span>)
546:                 <span class="ruby-identifier">entry</span> = <span class="ruby-identifier">table</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">byte</span>)
547:               <span class="ruby-keyword kw">else</span>
548:                 <span class="ruby-identifier">entry</span> = <span class="ruby-identifier">table</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">prevbyte</span>)
549:                 <span class="ruby-identifier">entry</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">entry</span>[<span class="ruby-value">0</span>,<span class="ruby-value">1</span>]
550:               <span class="ruby-keyword kw">end</span>
551: 
552:               <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">entry</span>
553:               <span class="ruby-identifier">table</span>[<span class="ruby-identifier">table</span>.<span class="ruby-identifier">index</span>(<span class="ruby-identifier">prevbyte</span>) <span class="ruby-operator">+</span> <span class="ruby-identifier">entry</span>[<span class="ruby-value">0</span>,<span class="ruby-value">1</span>]] = <span class="ruby-identifier">table</span>.<span class="ruby-identifier">size</span>
554:               <span class="ruby-identifier">prevbyte</span> = <span class="ruby-identifier">byte</span>
555:             <span class="ruby-keyword kw">end</span>
556:           <span class="ruby-keyword kw">end</span>
557:         <span class="ruby-keyword kw">end</span>
558:  
559:         <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@params</span> <span class="ruby-keyword kw">and</span> <span class="ruby-keyword kw">not</span> <span class="ruby-ivar">@params</span>[<span class="ruby-identifier">:Predictor</span>].<span class="ruby-identifier">nil?</span>
560:           <span class="ruby-identifier">colors</span> =  <span class="ruby-ivar">@params</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:Colors</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@params</span>[<span class="ruby-identifier">:Colors</span>].<span class="ruby-identifier">to_i</span> <span class="ruby-operator">:</span> <span class="ruby-value">1</span>
561:           <span class="ruby-identifier">bpc</span> =     <span class="ruby-ivar">@params</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:BitsPerComponent</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@params</span>[<span class="ruby-identifier">:BitsPerComponent</span>].<span class="ruby-identifier">to_i</span> <span class="ruby-operator">:</span> <span class="ruby-value">8</span>
562:           <span class="ruby-identifier">columns</span> = <span class="ruby-ivar">@params</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:Columns</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@params</span>[<span class="ruby-identifier">:Columns</span>].<span class="ruby-identifier">to_i</span> <span class="ruby-operator">:</span> <span class="ruby-value">1</span>
563: 
564:           <span class="ruby-identifier">result</span> = <span class="ruby-constant">Predictor</span>.<span class="ruby-identifier">do_post_prediction</span>(<span class="ruby-identifier">result</span>, <span class="ruby-ivar">@params</span>[<span class="ruby-identifier">:Predictor</span>].<span class="ruby-identifier">to_i</span>, <span class="ruby-identifier">colors</span>, <span class="ruby-identifier">bpc</span>, <span class="ruby-identifier">columns</span>)
565:         <span class="ruby-keyword kw">end</span>
566: 
567:         <span class="ruby-identifier">result</span>
568:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000316" class="method-detail">
        <a name="M000316"></a>

        <div class="method-heading">
          <a href="#M000316" class="method-signature">
          <span class="method-name">encode</span><span class="method-args">(string)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Encodes given data using <a href="LZW.html">LZW</a> compression method.
</p>
<table>
<tr><td valign="top"><em>stream</em>:</td><td>The data to <a href="LZW.html#M000316">encode</a>.

</td></tr>
</table>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000316-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000316-source">
<pre>
     <span class="ruby-comment cmt"># File origami/filters.rb, line 459</span>
459:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">encode</span>(<span class="ruby-identifier">string</span>)
460:   
461:         <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@params</span> <span class="ruby-keyword kw">and</span> <span class="ruby-keyword kw">not</span> <span class="ruby-ivar">@params</span>[<span class="ruby-identifier">:Predictor</span>].<span class="ruby-identifier">nil?</span>
462:           <span class="ruby-identifier">colors</span> =  <span class="ruby-ivar">@params</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:Colors</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@params</span>[<span class="ruby-identifier">:Colors</span>].<span class="ruby-identifier">to_i</span> <span class="ruby-operator">:</span> <span class="ruby-value">1</span>
463:           <span class="ruby-identifier">bpc</span> =     <span class="ruby-ivar">@params</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:BitsPerComponent</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@params</span>[<span class="ruby-identifier">:BitsPerComponent</span>].<span class="ruby-identifier">to_i</span> <span class="ruby-operator">:</span> <span class="ruby-value">8</span>
464:           <span class="ruby-identifier">columns</span> = <span class="ruby-ivar">@params</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">:Columns</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@params</span>[<span class="ruby-identifier">:Columns</span>].<span class="ruby-identifier">to_i</span> <span class="ruby-operator">:</span> <span class="ruby-value">1</span>
465: 
466:           <span class="ruby-identifier">string</span> = <span class="ruby-constant">Predictor</span>.<span class="ruby-identifier">do_pre_prediction</span>(<span class="ruby-identifier">string</span>, <span class="ruby-ivar">@params</span>[<span class="ruby-identifier">:Predictor</span>].<span class="ruby-identifier">to_i</span>, <span class="ruby-identifier">colors</span>, <span class="ruby-identifier">bpc</span>, <span class="ruby-identifier">columns</span>)
467:         <span class="ruby-keyword kw">end</span>       
468:         
469:         <span class="ruby-identifier">codesize</span> = <span class="ruby-value">9</span>
470:         <span class="ruby-identifier">result</span> = <span class="ruby-identifier">byte2binary</span>(<span class="ruby-constant">CLEARTABLE</span>)
471:         <span class="ruby-identifier">table</span> = <span class="ruby-identifier">clear</span>({})
472:         
473:         <span class="ruby-identifier">s</span> = <span class="ruby-value str">''</span>        
474:         <span class="ruby-identifier">string</span>.<span class="ruby-identifier">each_byte</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">byte</span><span class="ruby-operator">|</span>
475:           <span class="ruby-identifier">char</span> = <span class="ruby-identifier">byte</span>.<span class="ruby-identifier">chr</span>
476:           
477:           <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">table</span>.<span class="ruby-identifier">size</span>
478:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">512</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">codesize</span> = <span class="ruby-value">10</span>
479:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">1024</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">codesize</span> = <span class="ruby-value">11</span>
480:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">2048</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">codesize</span> = <span class="ruby-value">12</span>
481:             <span class="ruby-keyword kw">when</span> <span class="ruby-value">4096</span>
482:               <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">byte2binary</span>(<span class="ruby-constant">CLEARTABLE</span>,<span class="ruby-identifier">codesize</span>)
483:               <span class="ruby-identifier">codesize</span> = <span class="ruby-value">9</span>
484:               <span class="ruby-identifier">clear</span> <span class="ruby-identifier">table</span>
485:               <span class="ruby-keyword kw">redo</span>
486:           <span class="ruby-keyword kw">end</span>
487:          
488:           <span class="ruby-identifier">it</span> = <span class="ruby-identifier">s</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">char</span>
489:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">table</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">it</span>)
490:             <span class="ruby-identifier">s</span> = <span class="ruby-identifier">it</span>
491:           <span class="ruby-keyword kw">else</span>
492:             <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">byte2binary</span>(<span class="ruby-identifier">table</span>[<span class="ruby-identifier">s</span>], <span class="ruby-identifier">codesize</span>)
493:             <span class="ruby-identifier">table</span>[<span class="ruby-identifier">it</span>] = <span class="ruby-identifier">table</span>.<span class="ruby-identifier">size</span>
494:             <span class="ruby-identifier">s</span> = <span class="ruby-identifier">char</span>
495:           <span class="ruby-keyword kw">end</span>
496:         <span class="ruby-keyword kw">end</span>
497:          
498:         <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">byte2binary</span>(<span class="ruby-identifier">table</span>[<span class="ruby-identifier">s</span>], <span class="ruby-identifier">codesize</span>)
499:         <span class="ruby-identifier">result</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">byte2binary</span>(<span class="ruby-constant">EOD</span>,<span class="ruby-identifier">codesize</span>)
500:         
501:         <span class="ruby-identifier">binary2string</span>(<span class="ruby-identifier">result</span>)
502:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>